$(function() {
	var rows_delete=0;
	var mode="add";
	var m_id=0;
    var url='controls/time_model.php';
    $("#txtDateFrom_s,#txtDateTo_s").val(_cdate);
	LoadData(0);
	
	$('.btndate').datepicker();
	
	 function LoadData(id){
        $( "#resultData tbody tr" ).remove();
 
        
        $('#resultData').dataTable( {
			"bProcessing": true,
			"bFilter": false,
			"bLengthChange": false,
			"sAjaxSource": url,
			"oLanguage": {
				"sEmptyTable":     "ไม่พบข้อมูล"
			},
			"bDestroy": true,
			"sServerMethod": "POST",
			"fnServerParams": function ( aoData ) {
					aoData.push( { "name": "m", "value": "load" },
								{"name":"id","value":id}
							);
				},
			"aoColumnDefs":[
				{  "bVisible": false , "aTargets": [ 1 ] },	
			 	{  "bSortable": true , "aTargets": [ 2 ] },	
				{  "bVisible": false , "aTargets": [ 3 ] },	
				{   "bSortable": true, "aTargets": [ 4 ] ,"sClass":"left"}
				  
			],"fnDrawCallback": function ( oSettings ) {
				 var that = this;
				/* Need to redo the counters if filtered or sorted */
                 var iTotalRecords = oSettings.fnRecordsTotal();
				//alert(oSettings.aiDisplay.length + ' ' + iTotalRecords);
				 
					if (iTotalRecords > 0){ 	
						if ( oSettings.bSorted || oSettings.bFiltered )
						{
							for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
							{
								$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
							}
						}
					}
					 
				} 
		 } );
		 
      }
	
 
	
	$("#btnSearch").click(function(){
	
		var _name = $("#txtName_s").val();
		var _df = $("#txtDateFrom_s").val();
		var _dt = $("#txtDateTo_s").val();
		
		LoadData(_name,_df,_dt);
	});
	

	$("#btnSave").live('click',function(){
		$("input").not("[type=submit]").jqBootstrapValidation({preventSubmit: false,
			submitSuccess: function ($form, event) { event.preventDefault();}
		}); 
	  
		 
		 $("input").jqBootstrapValidation("destroy");
	});		
	 
	$("#btnAdd").live('click', function(){
		mode="add";
		 
	});
		
   
	  
    
});


